package jjn.carl.binary_tree;

import commons.BinaryTreeHelper;
import commons.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Jiang Jining
 * @since 2023-07-14 23:46
 */
public class LeetCode111 {
    public int minDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.left == null || root.right == null) {
            return root.left == null ? minDepth(root.right) + 1 : minDepth(root.left) + 1;
        }
        int minLeft = minDepth(root.left);
        int minRight = minDepth(root.right);
        return Math.min(minLeft, minRight) + 1;
    }
    
    public static void main(String[] args) {
        List<Integer> input = new ArrayList<>();
        input.addAll(List.of(3, 9, 20));
        input.add(null);
        input.add(null);
        input.addAll(List.of(15, 7));
        TreeNode node = BinaryTreeHelper.buildTree(input);
        int minDepth = new LeetCode111().minDepth(node);
        System.out.println("minDepth = " + minDepth);
    }
}
